﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using GtsServer.Common;
using System.Data.SqlClient;

namespace GtsServer.DAL
{
    public partial class LotteryPoolDAL
    {
        public LotteryPoolDAL()
        { }
        #region  Method

        /// <summary>
        /// 得到最大ID  
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ID", "LotteryPool");
        }

        /// <summary>
        /// 彩池模式开始游戏
        /// </summary>
        /// <param name="userAccount"></param>
        /// <param name="totalbet">总共押注数</param>
        /// <param name="totalResult">压中的分数  用来兑换彩蛋的</param>
        /// <returns></returns>
        public DataSet EditLotteryPool(string userAccount, int totalbet, int totalResult)
        {
            IDataParameter[] parms = new IDataParameter[]{ 
                new SqlParameter("@userAccount",userAccount),
                new SqlParameter("@totalbet",totalbet),
                new SqlParameter("@totalResult",totalResult)
            };
            DataSet ds = DbHelperSQL.RunProcedure("EditLotteryPool", parms, "EditLotteryPool");
            return ds;
        }


        /// <summary>
        /// 彩池模式排行（根据彩蛋总数排行）
        /// </summary>
        /// <param name="userAccount"></param>
        /// <returns></returns>
        public DataSet LotteryPoolRank(string userAccount)
        {
            IDataParameter[] parms = new IDataParameter[]{ 
                new SqlParameter("@userAccount",userAccount)
            };
            DataSet ds = DbHelperSQL.RunProcedure("LotteryPoolRank", parms, "LotteryPoolRank");
            return ds;
        }
        
        /// <summary>
        /// 进入彩池模式
        /// </summary>
        /// <param name="userAccount"></param>
        /// <returns></returns>
        public DataSet LotteryPoolInit(string userAccount)
        {
            IDataParameter[] parms = new IDataParameter[]{ 
                new SqlParameter("@userAccount",userAccount)
            };
            DataSet ds = DbHelperSQL.RunProcedure("LotteryPoolLogin", parms, "LotteryPoolLogin");
            return ds;
        }
        /// <summary>
        /// 彩池模式比大小
        /// </summary>
        /// <param name="userAccount"></param>
        /// <param name="prizeCount"></param>
        /// <param name="guess">0:未猜中  1：猜中</param>
        /// <returns></returns>
        public DataSet LotteryPoolGuess(string userAccount, int prizeCount, int guess)
        {
            IDataParameter[] parms = new IDataParameter[]{ 
                new SqlParameter("@userAccount",userAccount),
                new SqlParameter("@prizeCount",prizeCount),
                new SqlParameter("@guessValue",guess)
            };
            DataSet ds = DbHelperSQL.RunProcedure("LotteryPoolGuess", parms, "LotteryPoolGuess");
            return ds;
        }



        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from LotteryPool");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 更改奖池中的彩蛋数
        /// </summary>
        /// <param name="editNum"></param>
        /// <returns></returns>
        public bool UpdatePoolNum(int editNum)
        {
            string sql = "update LotteryPool set PoolNum=PoolNum+" + editNum + " where IsOpen=1";
            int rows = DbHelperSQL.ExecuteSql(sql);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(GtsServer.Model.LotteryPool model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update LotteryPool set ");
            strSql.Append("Name=@Name,");
            strSql.Append("PoolNum=@PoolNum,");
            strSql.Append("EditTime=@EditTime,");
            strSql.Append("EditNum=@EditNum,");
            strSql.Append("PoolBet=@PoolBet,IsOpen=@IsOpen,Remark=@Remark");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.VarChar,50),
					new SqlParameter("@PoolNum", SqlDbType.Int,4),
					new SqlParameter("@EditTime", SqlDbType.Int,4),
					new SqlParameter("@EditNum", SqlDbType.Int,4),
					new SqlParameter("@PoolBet", SqlDbType.Int,4),
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@IsOpen", SqlDbType.Bit,1),
					new SqlParameter("@Remark", SqlDbType.VarChar,200)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.PoolNum;
            parameters[2].Value = model.EditTime;
            parameters[3].Value = model.EditNum;
            parameters[4].Value = model.PoolBet;
            parameters[5].Value = model.ID;
            parameters[6].Value = model.IsOpen;
            parameters[7].Value = model.Remark;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from LotteryPool ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
            parameters[0].Value = ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from LotteryPool ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,Name,PoolNum,EditTime,EditNum,PoolBet,IsOpen,Remark ");
            strSql.Append(" FROM LotteryPool ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion  Method
    }
}
